package com.jkdz.spyke.widgetUIs {
	import flash.utils.ByteArray;
	import spark.components.Group;
	import spark.components.Label;
	import spark.components.VScrollBar;
	import spark.layouts.HorizontalAlign;
	import spark.layouts.VerticalAlign;
	import spark.layouts.VerticalLayout;
	import spark.primitives.Rect;

	import com.jkdz.spyke.flexUI.helpers.FileDropListener;
	import com.jkdz.spyke.flexUI.helpers.FileDropManager;
	import com.jkdz.spyke.flexUI.helpers.FileDropReference;
	import com.jkdz.spyke.flexUI.helpers.MouseWheelManager;
	import com.jkdz.spyke.flexUI.helpers.SuperWindow;
	import com.jkdz.spyke.flexUI.main.Main;
	import com.jkdz.spyke.rs.FileInfo;
	import com.jkdz.spyke.rs.SpykeFileSharer;
	import com.jkdz.spyke.rs.SpykeFileSharerListener;
	import com.jkdz.spyke.widgetUIs.fileSharer.SpykeFileProviderAdapter;
	import com.jkdz.spyke.widgetUIs.fileSharer.SpykeOfferedFileIcon;

	import mx.binding.utils.BindingUtils;
	import mx.core.FlexGlobals;
	import mx.events.FlexEvent;
	import mx.graphics.SolidColor;
	import mx.graphics.SolidColorStroke;

	import flash.events.Event;
	import flash.filters.DropShadowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class SpykeFileSharerUI extends SpykeWidgetUI implements SpykeFileSharerListener {
		
		private static const STANDARD_SIZE:int = 300;
		
		private var widget:SpykeFileSharer;
		private var window:SuperWindow;
		private var area:Group;
		private var files:Object;
		private var scroll:VScrollBar;
		
		public function SpykeFileSharerUI(widget:SpykeFileSharer) {
			this.widget = widget;
			this.files = new Object();
		}
		
		public function folderNameChanged(newName:String):void {
			window.title = newName;
		}
		
		override public function display(onComplete:Function):void {
			
			widget.getName(function(name:String):void {
				showUI(name, onComplete);
			});
		}
		
		public function newFileOffered(info:FileInfo):void {
			var sofi:SpykeOfferedFileIcon = new SpykeOfferedFileIcon();
			sofi.codeInit(info, STANDARD_SIZE-40, this);
			files[info.id] = sofi;
			area.addElement(sofi);
			callLater(function():void {
				scroll.value = scroll.maximum;
			});
		}
		
		public function fileUnOffered(fid:String):void {
			var sofi:SpykeOfferedFileIcon = files[fid];
			if (sofi != null) {
				delete files[fid];
				area.removeElement(sofi);
			}
		}
		
		public function unPublished():void {
			if (window.isVisible()) {
				window.close();
			}
		}
		
		private function showUI(name:String, onComplete:Function):void {
			
			var vl:VerticalLayout = new VerticalLayout();
			vl.gap = 10;
			vl.horizontalAlign = VerticalAlign.MIDDLE;
			vl.paddingBottom = vl.paddingTop = vl.paddingRight = vl.paddingLeft = 10;
			this.layout = vl;
			
			var areaHolder:Group = new Group();
			var reactRectHolder:Group = new Group();
			var reactRect:Rect = new Rect();
			reactRect.fill = new SolidColor(0xffcccccc);
			reactRect.stroke = new SolidColorStroke(0xff000000);
			reactRect.percentWidth = 100.0;
			reactRect.percentHeight = 100.0;
			reactRectHolder.addElement(reactRect);
			var infoGroup:Group = new Group();
			infoGroup.percentWidth = 100.0;
			infoGroup.percentHeight = 100.0;
			var centerer:VerticalLayout = new VerticalLayout();
			centerer.verticalAlign = VerticalAlign.MIDDLE;
			centerer.horizontalAlign = HorizontalAlign.CENTER;
			infoGroup.layout = centerer;
			var infoText:Label = new Label();
			infoText.text = "Drop files here";
			infoText.setStyle("color", 0xfffefefe);
			infoText.setStyle("fontSize", 28);
			infoGroup.addElement(infoText);
			reactRectHolder.addElement(infoGroup);
			areaHolder.addElement(reactRectHolder);
			area = new Group();
			var areal:VerticalLayout = new VerticalLayout();
			areal.paddingBottom = areal.paddingLeft = areal.paddingRight = areal.paddingTop = areal.gap = 0;
			area.layout = areal;
			area.clipAndEnableScrolling = true;
			area.x = 1;
			area.y = 1;
			scroll = new VScrollBar();
			scroll.viewport = area;
			scroll.x = STANDARD_SIZE - 38;
			scroll.y = 1;
			areaHolder.addElement(area);
			areaHolder.addElement(scroll);
			this.addElement(areaHolder);
			
			FileDropManager.addFileDropListener(areaHolder, new FileDropListener(function(fdr:FileDropReference):void {
				var sofi:SpykeOfferedFileIcon = null;
				widget.offerFile(fdr.getName(), fdr.getLength(), new SpykeFileProviderAdapter(function(from:int, suggestedLength:int, onRead:Function):void {
						sofi.setActivity(true);
						fdr.getChunk(from, suggestedLength, function(ret:ByteArray):void {
							onRead(ret);
							sofi.setActivity(false);
						});
					}), function(fid:String):void {
					sofi = files[fid];
					if (sofi != null) {
						sofi.setOwned(true);
					}
				});
			}, function(over:Boolean):void {
				infoText.setStyle("fontSize", over ? 32 : 28);
				infoText.filters = over ? [new DropShadowFilter(0, 0, 0xffeeeeee, 1.0, 32, 32, 4, 3)] : [];
			}, function(near:Boolean):void {
				reactRect.fill = new SolidColor(near ? 0xffaaaaaa : 0xffcccccc);
			}));
			
			this.window = SuperWindow.display(FlexGlobals.topLevelApplication as Main, this, name, false, true, true, true, new Point(100+Math.random()*((FlexGlobals.topLevelApplication as Main).width-500), 100+Math.random()*((FlexGlobals.topLevelApplication as Main).height-500)), onClose);
			window.setSizeConstraints(new Rectangle(STANDARD_SIZE, 140, STANDARD_SIZE, 10000));
		
			window.addEventListener(FlexEvent.CREATION_COMPLETE, function(e:Event):void {
				var adjuster:Function = function(v:Number):void {
					reactRectHolder.width = area.width = window.width-22;
					reactRectHolder.height = window.height-55;
					scroll.height = area.height = reactRectHolder.height-2;
				};
				BindingUtils.bindSetter(adjuster, window, "width");
				BindingUtils.bindSetter(adjuster, window, "height");
				
				window.width = STANDARD_SIZE;
				window.height = STANDARD_SIZE;
			});

			widget.getFiles(function(fileInfos:Vector.<*>):void {
				for each (var file:FileInfo in fileInfos) {
					newFileOffered(file);
				}
			});
			
			MouseWheelManager.addMouseWheelListener(this, function(atX:int, atY:int, delta:int):void {
				scroll.value -= delta*20;
			});
			
			widget.registerListener(this);

			onComplete();
		}
		
		private function onClose():void {
			widget.unPublish();
			MouseWheelManager.removeMouseWheelListener(this);
		}

		public function onDelete(id:String):void {
			widget.unOfferFile(id);
		}

	}
}